/**
 * Created by wry on 2015/4/13.
 */
angular.module('backofficeApp.mutualAidManager.controllers', []).
    controller('mutualAidCtrl', ['$scope', 'CommunityService', '$http', 'MasterDataService', function ($scope, communityService, $http, masterDataService) {

        $scope.communityLabelList = communityService.getAllCommunityService();
        $scope.tag = masterDataService.getAllDicService({category: '02'});

        $scope.currentPage = 1;
        $scope.totalPage = 0;
        $scope.pages = [];

        var table = 1;

        $scope.getActivityList = function (communityId, flag, level) {
            if (!flag) {
                $scope.currentPage = 1;
            }
            $http.get('http://' + backOfficeHost + '/rest/v1.0/backoffice/activity/getBackOfficeActivityList/' + communityId + '/' + $scope.currentPage + '/' + level, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                table = 2;
                $scope.totalPage = data.data.totalPage;

                var result = data.data.aList;

                $http.get('http://' + backOfficeHost + '/rest/v1.0/backoffice/question/getFreshNewsQuestion', {
                    headers: {'Content-Type': 'application/json;charset=utf-8'}
                }).success(function (data) {
                    $scope.freshQuestion = data.data;

                    result.forEach(function (question) {
                        $scope.freshQuestion.forEach(function (fresh) {
                            if (question.objId == fresh.newsId) {
                                question['isFresh'] = true
                            }
                        });
                    });
                    $scope.activityList = result;
                }).error(function () {
                });

            }).error(function (data) {
            });
        };

        $scope.getAllUgc = function (communityId, flag) {
            if (!flag) {
                $scope.currentPage = 1;
            }
            $http.get('http://' + backOfficeHost + '/rest/v1.0/backoffice/activity/getAllUgc/' + communityId + '/' + $scope.currentPage, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                table = 1;
                $scope.allList = data.data.allList;
                $scope.totalPage = data.data.totalPage;
            }).error(function (data) {
            });
        };

        $scope.getList = function (communityId, level) {
            if (table == 1) {
                $scope.getAllUgc(communityId, false)
            } else if (table == 2) {
                $scope.getActivityList(communityId, false, level);
            }
        };

        //置顶
        $scope.moveToTop = function (activityId, communityId) {
            var opt = {
                activityId: activityId,
                communityId: communityId
            };
            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/activity/moveActivityToTop', opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.getActivityList(communityId, true);
            }).error(function () {
            });
        };

        $scope.cancelTop = function (activityId, communityId) {
            var opt = {
                activityId: activityId
            };
            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/activity/cancelTopActivity', opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.getActivityList(communityId, true);
            }).error(function () {

            });
        };

        $scope.moveAllToTop = function (id, communityId) {
            var opt = {
                id: id,
                community: communityId
            };
            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/activity/moveAllToTop', opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.getAllUgc(communityId, true);
            }).error(function () {
            });
        };

        $scope.cancelAllTop = function (id, communityId) {
            var opt = {
                id: id
            };
            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/activity/cancelTopAll', opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.getAllUgc(communityId, true);
            }).error(function () {

            });
        };

        $scope.onSelectPage = function (page) {
            $scope.currentPage = page;
            if (table == 1) {
                $scope.getAllUgc($scope.communityId, true)
            } else if (table == 2) {
                $scope.getActivityList($scope.communityId, true);
            }
        }

        $scope.data = [];
        $scope.toggle = function (ck, objId) {
            !ck ? $scope.data.push(objId) : $scope.data.remove(objId);
        };

        $scope.initTag = function () {
            $scope.data = [];
        };

        //更新问题
        $scope.updateActivity = function (objId) {
            var temp = {
                "objId": objId,
                "tag": $scope.data
            };

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/activity/backOfficeUpdateActivity', temp, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.getActivityList($scope.communityId, true);
            }).error(function () {
            });
        };

        $scope.addActivityToFreshNews = function (activity, title) {
            if (title == null) {
                alert('请输入标题');
                return;
            }
            activity.desc = title;

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/marketing/addActivityToFreshNews', activity, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.getActivityList($scope.communityId, true);
            }).error(function () {
            });
        }

        $scope.changeLevel = function (id, level) {
            var input = {
                activityId: id,
                level: level
            };

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/activity/changeActivityLevel', input, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.getActivityList($scope.communityId, true);
            }).error(function () {
            });
        }


    }]).directive('mutau', function () {
        return {
            restrict: 'E',
            //scope: {
            //    numPages: '=',
            //    currentPage: '=',
            //    onSelectPage: '&'
            //},
            template: '',
            replace: true,
            link: function (scope, element, attrs) {
                scope.$watch('currentPage', function (value) {
                    scope.pages = [];
                    for (var i = value - 5 > 0 ? value - 5 : 1; i <= (value + 5 > scope.totalPage ? scope.totalPage : value + 5); i++) {
                        scope.pages.push(i);
                    }
                });
                scope.$watch('totalPage', function (value) {
                    scope.pages = [];
                    for (var i = scope.currentPage - 5 > 0 ? scope.currentPage - 5 : 1; i <= (scope.currentPage + 5 > value ? value : scope.currentPage + 5); i++) {
                        scope.pages.push(i);
                    }
                });

                scope.isActive = function (page) {
                    return scope.currentPage === page;
                };
                scope.selectPage = function (page) {
                    if (!scope.isActive(page)) {
                        scope.currentPage = page;
                        scope.onSelectPage(page);
                    }
                };
                scope.selectPrevious = function () {
                    if (!scope.noPrevious()) {
                        scope.selectPage(scope.currentPage - 1);
                    }
                };
                scope.selectNext = function () {
                    if (!scope.noNext()) {
                        scope.selectPage(scope.currentPage + 1);
                    }
                };
                scope.noPrevious = function () {
                    return scope.currentPage == 1;
                };
                scope.noNext = function () {
                    return scope.currentPage == scope.totalPage;
                };

            }
        };
    });